package com.original.simuladores.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.original.simuladores.entities.Product;
import com.original.simuladores.service.ProductService;

@RestController
public class ProductController {
	@Autowired
	ProductService productService;
	
	@RequestMapping(value = "/products", method = RequestMethod.GET, headers = "Accept=application/json")
	public List<Product> getAllProducts() {
		return productService.getProducts();
	}
	
	/*@RequestMapping(value = "/products/{productId}", method = RequestMethod.GET, headers = "Accept=application/json")
	public Product getProductById(@PathVariable int productId) {
		Product product = new Product();
		List<Product> products = productService.getProductById(productId);
		if (products != null && products.size() > 0) {
			product = products.get(0);
		}
		return product;
	}*/
	
	@RequestMapping(value = "/products/delete/{productId}", method = RequestMethod.GET, headers = "Accept=application/json")
	public List<Product> deleteProduct(@PathVariable Integer productId) {
		Product prt = new Product();
		prt.setId(productId);
		productService.delete(prt);
		return productService.getProducts();
	}
	
	@RequestMapping(value = "/products/insert/{productName}/{productDescription}/{productTaxAmount}/{productFgAllowCustomAmount}/{productFgActive}/{productVlCalcType}"
			, method = RequestMethod.GET, headers = "Accept=application/json")
	public List<Product> insertProduct(@PathVariable String productName
			, @PathVariable String productDescription
			, @PathVariable Float productTaxAmount
			, @PathVariable Boolean productFgAllowCustomAmount
			, @PathVariable Boolean productFgActive
			, @PathVariable Integer productVlCalcType) {
		Product prt = new Product();
		prt.setId(productService.getId());
//		prt.setId(null);
		prt.setName(productName);
		prt.setDescription(productDescription);
		prt.setTax_Amount(productTaxAmount);
		prt.setFg_Allow_Custom_Amount(productFgAllowCustomAmount);
		prt.setFg_Active(productFgActive);
		prt.setVlCalcType(productVlCalcType);
		productService.create(prt);
		return productService.getProducts();
	}
	
	@RequestMapping(value = "/products/update/{productId}/{productName}/{productDescription}/{productTaxAmount}/{productFgAllowCustomAmount}/{productFgActive}/{productVlCalcType}"
			, method = RequestMethod.GET, headers = "Accept=application/json")
	public List<Product> updateProduct(@PathVariable Integer productId
			, @PathVariable String productName
			, @PathVariable String productDescription
			, @PathVariable Float productTaxAmount
			, @PathVariable Boolean productFgAllowCustomAmount
			, @PathVariable Boolean productFgActive
			, @PathVariable Integer productVlCalcType) {
		
		Product prt = new Product();
		
		prt.setId(productId);
		prt.setName(productName);
		prt.setDescription(productDescription);
		prt.setTax_Amount(productTaxAmount);
		prt.setFg_Allow_Custom_Amount(productFgAllowCustomAmount);
		prt.setFg_Active(productFgActive);
		prt.setVlCalcType(productVlCalcType);
		productService.update(prt);
		return productService.getProducts();
	}
	
}
